<?php
// 初始化结果变量
$result = '';

// 十二生肖数组
$zodiacs = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];

// 获取当前年份
$currentYear = (int)date('Y');

// 已知 2024 年为龙年
$baseYear = 2024;
$baseZodiacIndex = array_search('龙', $zodiacs);

// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $zodiac = $_POST['zodiac'];
    $zodiacIndex = array_search($zodiac, $zodiacs);

    if ($zodiacIndex!== false) {
        $yearDiff = ($zodiacIndex - $baseZodiacIndex + 12) % 12;
        $approximateYear = $baseYear + $yearDiff;

        // 找到距离当前年份较近的可能出生年份
        $possibleYears = [];
        for ($i = -10; $i <= 3; $i++) {
            $year = $approximateYear + $i * 12;
            if ($year > 0) {
                $possibleYears[] = $year;
            }
        }

        sort($possibleYears);

        $result = "生肖 “{$zodiac}” 可能的出生年份是：" . implode(', ', $possibleYears) . " 年。";
    } else {
        $result = "输入的生肖信息有误，请检查后重新输入。";
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>十二生肖算出生年份游戏</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <h1>十二生肖算出生年份游戏</h1>
    <form method="post">
        <label for="zodiac">请输入生肖（如：龙）：</label>
        <input type="text" id="zodiac" name="zodiac" required>
        <input type="submit" value="计算出生年份">
    </form>
    <?php if ($result): ?>
        <div id="result">
            <p><?php echo $result; ?></p>
        </div>
    <?php endif; ?>
</body>

</html>